/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_FileRead.
	/// </summary>
	public class Block_FileRead : Block
	{

		public enum READ_TYPE
		{
			UserDefined = 1,
			CompleteContent = 2,
			Line = 3
		}

		private READ_TYPE _readType;
		private string _fileHandle;
		private string _startPos;
		private string _storeDataInto;
		private string _length;
		private string _storeNoCharsInto;
		private string _EOLType;

		public override TYPE GetBlockType()
		{
			return TYPE.FileRead;
		}

		public override string GetXML(int x, int y)
		{
            /* TODO:
            Trace.Debug("Block_Delay::GetXML Started.");	
            // <Delay name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<Delay name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
            //  ...description="DESC"...
            xml += GetDescriptionXML();
            //  ...clearDTMF="_clearDTMFBuffer"...
            xml += GetClearDTMFXML();
            // ...minUserLevel="_userLevel"
            xml += GetMinUserLevelXML();
            // ...cpMessage=""...
            xml += GetControlPanelMessageXML();
            // ...delayPeriod="timeout"...
            xml += " delayPeriod=\"" + XmlString(_delayPeriod) + "\"";
            // ...maxSilence="_silenceDetection" >
            xml += " maxSilence=\"" + XmlString(_silenceDetection) + "\">\r\n";

            // <Inputs>
            xml += GetInputsXML(_blockId);
		
            // <Outputs>
            xml += GetOutputsXML();

            // <Logs>
            xml += GetLogsXML();
			
            xml += GetHandledErrorsXML();

            // </Delay>
            xml +="\t\t\t</Delay>\r\n";
            return xml;*/
            return "";
		}

		// When we find a FileReadNext block, data comes in here.
		public override void AppendNextData(byte[] rawData)
		{
			Trace.Debug("Block_FileRead: Appending data");
			// Maybee need the id from this block as well?
// TODO: outputs.
			// 			outputErrorIds.Add(new BlockOutput(OUTPUT.Next, -1, 0));
			//outputErrorIds.Add(new BlockOutput(OUTPUT.NotFound_1, -1, 54));


			Settings.Instance.Unimplemented("Block_FileRead");
			// TODO: Not implemented at all
			AddToBlockNote("This block '" + _name + "' has not been fully implemented in the decompiler. Stuff will be missing.");
		}


		public Block_FileRead(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.
			Trace.Debug("Block_FileRead created.");

			// Type of read operation
			_readType = (READ_TYPE)rawData[_currentPos + 24];

			_currentPos += 28;

			// File handle
			_fileHandle = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// start position
			_startPos = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// store data into
			_storeDataInto = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// length
			_length = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// store number of chars into
			_storeNoCharsInto = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// EOL type
			_EOLType = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			Trace.Debug("FileRead: readType: " + _readType +
				", file handle: " + _fileHandle +
				", startPos: " + _startPos +
				", storeInto: " + _storeDataInto +
				", length: " + _length +
				", storeNoCharsInto: " + _storeNoCharsInto +
				", EOLType: " + _EOLType);
		}
	}

}